﻿using UnityEngine;using System.Collections;[RequireComponent (typeof(CharacterController))]public class ThirdPersonMotor : MonoBehaviour {

    private GameObject movingPlatform = null;
    private Vector3 lastHitPoint = Vector3.zero;
    private CharacterController characterController;    // Use this for initialization    void Start () {
        characterController = GetComponent<CharacterController>();    }        // Update is called once per frame    void Update () {        }

    void FixedUpdate() {
        if (movingPlatform != null) {
            Vector3 curPosition = movingPlatform.transform.position;
            characterController.Move(curPosition - lastHitPoint);
            lastHitPoint = curPosition;
        }
    }    void OnControllerColliderHit(ControllerColliderHit hit) {
        if (hit.normal.y > 0.0f) {
            GameObject newPlatform = hit.gameObject;
            if (newPlatform != movingPlatform) {
                lastHitPoint = newPlatform.transform.position;
                movingPlatform = newPlatform;
            }
        }    }}